var one=biFromHex('1');//常数1
var two=biFromHex('2');//常数2
//计算生成元
function get_g(P,Q)
{
	var g=two;//设置初始值为2
	while(biToHex(biPowMod(g,two,P))==biToHex(one)||biToHex(biPowMod(g,Q,P))==biToHex(one))	//g^2 mod P==1|| g^Q mod P==1
	{
		g=biAdd(g,one);	//g++
	}
	return g;
}
//获取p
function get_p(p)
{
	if(p)
	{
		return biFromHex(p);
	}
	//这里需要随机生成k
	return biFromHex('EBEC3D2016842A1AD7E553E735E1EB6F');
}
//计算Q
function get_q(P)
{
	//alert(P)
	Q=biSubtract(P,one); //P-1
	Q=biDivide(Q,two);	//(P-1)/2
	return Q;
}
//获取随机数x
function get_x(len)
{
	res=""
	for(var i=0;i<len;i++)
	{
		var id=Math.ceil(Math.random()*16)-1;
		res+=hexToChar[id];	//拼接一位
	}
	return biFromHex(res);//返回大数x
}
//计算y
function get_y(g,X,P)
{
	var Y=biPowMod(g,X,P);
	return Y;
}
//计算K
function get_k(X,Yb,P)
{
	return biPowMod(Yb,X,P);
}
//加密
function aes_enc(plain,k)
{
	var key = CryptoJS.enc.Utf8.parse(k);	
	//alert(key)
	var srcs =CryptoJS.enc.Utf8.parse(plain);
	var encrypted = CryptoJS.AES.encrypt(srcs, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
	return encrypted.toString();
}
//解密
function aes_dec(encrypt,k)
{
    var key = CryptoJS.enc.Utf8.parse(k)
    //alert(key)
   	var decrypt = CryptoJS.AES.decrypt(encrypt, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
	return CryptoJS.enc.Utf8.stringify(decrypt).toString();
}
//十六进制字符转换数字
function hex_to_value(c)
{
	//console.log(c)
	var v=c.charCodeAt(0);
	if(v>=48&&v<=58)
		return c-'0';
	else if(v>=65&&v<=91)
		return v-65+10;
	else
		return v-97+10;
}
//获取十六进制字串对应的字符串
function get_hex_to_char_str(str)
{
	var res="";
	for(var i=0;i<32;i+=2)
	{
		var value=hex_to_value(str[i])*16+hex_to_value(str[i+1]);
		//console.log("value "+i+"is"+value);
		res+=String.fromCharCode(value/2)
	}
	return res;
}